Imports System.Windows.Forms

Public Class frmSupportGroup
    Private SupportTable, GroupTable As String
    Private SupportField, GroupField As String
    Private HasChanged As Boolean = False
    Private LastGroupName As String = ""
    Friend InitialGroupType As Integer = 0

    Private Sub cboAvail_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAvail.SelectedIndexChanged
        Try
            Select Case cboAvail.SelectedIndex
                Case 0
                    SupportTable = "Stations"
                    SupportField = "Station_ID"
                    GroupTable = "StaGrp"
                    GroupField = "StationGroup"
                Case 1
                    SupportTable = "PCodes"
                    SupportField = "PCode"
                    GroupTable = "PCodeGrp"
                    GroupField = "PCodeGroup"
                Case 2
                    SupportTable = "Tracking"
                    SupportField = "Temp_DB_Name"
                    GroupTable = "Tracking"
                    GroupField = "TableGroup"
                Case Else
                    SupportTable = ""
                    SupportField = ""
                    GroupTable = ""
                    GroupField = ""
            End Select
            With lstAvail
                .Items.Clear()
                .Items.AddRange(Project.DB.GetRecordList(SupportTable, SupportField).ToArray)
            End With
            FillGroups()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub FillGroups()
        lstGroup.Items.Clear()
        With cboGroup
            .Text = ""
            .Items.Clear()
            .Items.AddRange(Project.DB.GetRecordList(GroupTable, GroupField).ToArray)
            If .Items.Count = 0 Then
                .Text = "New Group Name"
                LastGroupName = ""
            Else
                If LastGroupName <> "" Then
                    .Text = LastGroupName
                Else
                    .SelectedIndex = 0
                End If
                LastGroupName = .Text
            End If
        End With
        lstAvail.SelectedItems.Clear()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        SaveChanges(True)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        For Each s As String In lstAvail.SelectedItems
            If Not lstGroup.Items.Contains(s) Then lstGroup.Items.Add(s)
        Next
        HasChanged = True
    End Sub

    Private Sub btnAddAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAll.Click
        For Each s As String In lstAvail.Items
            If Not lstGroup.Items.Contains(s) Then lstGroup.Items.Add(s)
        Next
        HasChanged = True
    End Sub

    Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        With lstGroup
            For i As Integer = .Items.Count - 1 To 0 Step -1
                If .SelectedIndices.Contains(i) Then .Items.RemoveAt(i)
            Next
        End With
        HasChanged = True
    End Sub

    Private Sub btnRemoveAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveAll.Click
        lstGroup.Items.Clear()
        HasChanged = True
    End Sub

    Private Sub lstAvail_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstAvail.DoubleClick
        btnAdd.PerformClick()
    End Sub

    Private Sub lstGroup_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstGroup.DoubleClick
        btnRemove.PerformClick()
    End Sub

    Private Sub frmSupportGroup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cboAvail.SelectedIndex = InitialGroupType
    End Sub

    Private Sub SaveChanges(Optional ByVal SkipWarning As Boolean = False)
        Try
            If LastGroupName = "" Then Exit Sub
            If HasChanged AndAlso (SkipWarning OrElse MessageBox.Show("You have modified a group; do you want to save your changes before closing the form?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                With lstGroup
                    If cboAvail.SelectedIndex = 2 Then
                        Project.DB.ExecuteNonQuery("UPDATE {0} SET {1}=NULL WHERE {1}='{2}'", GroupTable, GroupField, LastGroupName)
                        For Each s As String In .Items
                            Project.DB.ExecuteNonQuery("UPDATE {0} SET {1}='{2}' WHERE {3}='{4}'", GroupTable, GroupField, LastGroupName, SupportField, s)
                        Next
                    Else
                        If Project.DB.ExecuteNonQuery("DELETE FROM {0} WHERE {1}='{2}'", GroupTable, GroupField, LastGroupName) = -2 Then Exit Sub
                        For Each s As String In .Items
                            If Project.DB.ExecuteNonQuery("INSERT INTO {0} ({1},{2}) VALUES ('{3}','{4}')", GroupTable, GroupField, SupportField, LastGroupName, s) = -2 Then Exit For
                        Next
                    End If
                End With
                HasChanged = False
                LastGroupName = cboGroup.Text
                FillGroups()
            End If
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        SaveChanges()
    End Sub

    Private Sub cboAvail_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAvail.SelectionChangeCommitted
        SaveChanges()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub cboGroup_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboGroup.KeyPress
        If e.KeyChar = Chr(13) Then
            lstAvail.Focus()
            e.Handled = True
        End If
    End Sub

    Private Sub cboGroup_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboGroup.Validated, cboGroup.SelectedIndexChanged
        Try
            cboGroup.Text = cboGroup.Text.ToUpper.Replace("'", "")
            SaveChanges()
            With lstGroup
                .Items.Clear()
                Dim dr As DbDataReader = Project.DB.ExecuteReader("SELECT DISTINCT {0} FROM {1} WHERE {2}='{3}'", SupportField, GroupTable, GroupField, cboGroup.Text)
                If dr Is Nothing Then Exit Sub
                While dr.Read
                    .Items.Add(dr.GetString(0))
                End While
                dr.Close()
            End With
            LastGroupName = cboGroup.Text
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        ShowHelp(Me, "4")
    End Sub
End Class
